<template>
  <div class="xtx-search-page">
    <div class="container">
      <xtx-bread>
        <xtx-bread-item to="/">首页</xtx-bread-item>
        <xtx-bread-item>搜索 "{{ $route.query.keyword }}" 的结果：</xtx-bread-item>
      </xtx-bread>
      <div class="wrapper">
        <!-- 筛选区 -->
        <search-sort  v-if="total !== 0" ref="sortCom" @sortEV="changeSortFn"></search-sort>
        <!-- 结果区 -->
        <div class="goods_result" v-if="total !== 0">
           <goods-item v-for="item in goodsList" :key="item.id" :goods="item"></goods-item>
        </div>
        <!-- 暂无结果区 -->
        <div class="none" v-if="total === 0">
          <img src="../../assets/none.png" alt="" />
          暂无数据
        </div>
        <!-- 分页区 -->
        <xtx-pagination
          :total="total"
          :current-page="queryObj.page"
          :page-size="queryObj.pageSize"
          @current-change="changePager"
        ></xtx-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import SearchSort from './components/searchSort.vue'
import goodsItem from '@/components/GoodsItem.vue'
import { ref, reactive } from 'vue'
import { getSearchGoodsListAPI } from '@/api'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
export default {
  name: 'xtx-search-page',
  components: {
    SearchSort,
    goodsItem
  },
  setup () {
    const goodsList = ref([]) // 搜索结果列表
    const route = useRoute()
    const total = ref(0) // 结果总数
    const sortCom = ref(null) // 筛选组件对象
    /* {
      page: 页码,
      pageSize: 每页数量,
      keyword: 搜索关键词,
      associatedIds: 联想词id数组,
      categoryId: 分类id,
      brandId: 品牌id,
      onlyDiscount: 布尔值只显示特惠,
      sortField: 排序字段,
      sortMethod: 排序规则('desc', 'asc')
    } */
    // 配置
    const queryObj = reactive({
      page: 1,
      pageSize: 10,
      keyword: route.query.keyword,
      sortField: null,
      sortMethod: null
    })
    // 请求接口
    const getSearchGoodsListFn = async () => {
      const res = await getSearchGoodsListAPI(queryObj)
      goodsList.value = res.result.pageData.items
      total.value = res.result.pageData.counts
    }
    // 筛选条件发生改变
    const changeSortFn = (sortObj) => {
      queryObj.sortField = sortObj.sortField
      queryObj.sortMethod = sortObj.sortMethod

      getSearchGoodsListFn()
    }
    // 检测路由参数改变
    onBeforeRouteUpdate((to, from, next) => {
      queryObj.keyword = to.query.keyword
      queryObj.sortField = ''
      queryObj.sortMethod = 'desc'
      console.log(sortCom.value.sortObj)
      // if (sortCom.value) {
      //   sortCom.value.sortObj.sortField = ''
      //   sortCom.value.sortObj.sortMethod = 'desc'
      // }
      getSearchGoodsListFn()
      next()
    })
    getSearchGoodsListFn()

    // 页码改变->事件方法
    const changePager = (pageNum) => {
      queryObj.page = pageNum

      getSearchGoodsListFn()
    }
    return {
      goodsList,
      total,
      changeSortFn,
      sortCom,
      queryObj,
      changePager
    }
  }
}
</script>

<style lang="less" scoped>
.xtx-search-page {
  .wrapper {
    background-color: #fff;
    padding: 0 25px;
    .goods-list {
      display: flex;
      flex-wrap: wrap;
      padding: 0 5px;
      li {
        margin-right: 20px;
        margin-bottom: 20px;
        &:nth-child(5n) {
          margin-right: 0;
        }
      }
    }
  }
}
// 商品结果取
.goods_result {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
// 暂无数据标签样式
.none {
  padding: 100px 0;
  text-align: center;
  color: #999;
  img {
    width: 100px;
    height: 100px;
    margin-right: 10px;
    object-fit: contain;
  }
}
</style>
